﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETPerformance.App_BusinessLogic.BasePages;

namespace ASPNETPerformance._02_JsAndCss {
    public partial class GzipCompression :BasePage {
        protected void Page_Load( object sender, EventArgs e ) {
            if ( !this.Page.IsPostBack ) {
                BindGrid( Server.MapPath( "~/_assets/css/" ), this.gvCssFilesByDebug );
                BindGrid( Server.MapPath( "~/_assets/css/Release/zip/" ), this.gvCssFilesByRelease );
                BindGrid( Server.MapPath( "~/_assets/js/" ), this.gvJsFilesByDebug );
                BindGrid( Server.MapPath( "~/_assets/js/Release/zip/" ), this.gvJsFilesByRelease );
            }
        }

        private void BindGrid( string path, GridView gv )
        {
            var currentDirInfo = new DirectoryInfo( path );
            var files = currentDirInfo.GetFiles();

            var fsItems = new List<FileSystemItemCS>( files.Length );


            foreach ( var file in files )
                fsItems.Add( new FileSystemItemCS( file ) );

            gv.DataSource = fsItems;
            gv.DataBind();
        }

        protected string DisplaySize( long? size ) {
            if ( size == null )
                return string.Empty;
            else {
                if ( size < 1024 )
                    return string.Format( "{0:N0} bytes", size.Value );
                else
                    return String.Format( "{0:N0} KB", size.Value / 1024 );
            }
        }    

        public class FileSystemItemCS {
            public FileSystemItemCS( FileInfo file ) {
                this.Name = file.Name;
                this.FullName = file.FullName;
                this.Size = file.Length;
                this.CreationTime = file.CreationTime;
                this.LastAccessTime = file.LastAccessTime;
                this.LastWriteTime = file.LastWriteTime;
                this.IsFolder = false;
            }

            public FileSystemItemCS( DirectoryInfo folder ) {
                this.Name = folder.Name;
                this.FullName = folder.FullName;
                this.Size = null;
                this.CreationTime = folder.CreationTime;
                this.LastAccessTime = folder.LastAccessTime;
                this.LastWriteTime = folder.LastWriteTime;
                this.IsFolder = true;
            }

            public string Name {
                get;
                set;
            }
            public string FullName {
                get;
                set;
            }
            public long? Size {
                get;
                set;
            }
            public DateTime CreationTime {
                get;
                set;
            }
            public DateTime LastAccessTime {
                get;
                set;
            }
            public DateTime LastWriteTime {
                get;
                set;
            }
            public bool IsFolder {
                get;
                set;
            }

            public string FileSystemType {
                get {
                    if ( this.IsFolder )
                        return "File folder";
                    else {
                        var extension = Path.GetExtension( this.Name );

                        if ( IsMatch( extension, ".txt" ) )
                            return "Text file";
                        else if ( IsMatch( extension, ".pdf" ) )
                            return "PDF file";
                        else if ( IsMatch( extension, ".doc", ".docx" ) )
                            return "Microsoft Word document";
                        else if ( IsMatch( extension, ".xls", ".xlsx" ) )
                            return "Microsoft Excel document";
                        else if ( IsMatch( extension, ".jpg", ".jpeg" ) )
                            return "JPEG image file";
                        else if ( IsMatch( extension, ".gif" ) )
                            return "GIF image file";
                        else if ( IsMatch( extension, ".png" ) )
                            return "PNG image file";
                        else if ( IsMatch( extension, ".css" ) )
                            return "web css file";
                        else if ( IsMatch( extension, ".js" ) )
                            return "web js file";
                        else if ( IsMatch( extension, ".gz" ) )
                            return "gzip compression file";


                        // If we reach here, return the name of the extension
                        if ( string.IsNullOrEmpty( extension ) )
                            return "Unknown file type";
                        else
                            return extension.Substring( 1 ).ToUpper() + " file";
                    }
                }
            }

            private bool IsMatch( string extension, params string[] extensionsToCheck ) {
                foreach ( var str in extensionsToCheck )
                    if ( string.CompareOrdinal( extension, str ) == 0 )
                        return true;

                // If we reach here, no match
                return false;
            }
        }
    }
}